package com.example.demo.leetcode;

import org.junit.Test;

import java.util.Arrays;

public class leetcode137 {

    @Test
    public void test() {
        int[] arr = {-2, -2, 1, 1, 4, 1, 4, 4, -4, -2};
        System.out.println(singleNumber(arr));
    }

    public int singleNumber(int[] nums) {
        int len = nums.length;
        if (len < 2) {
            return nums[0];
        }

        Arrays.sort(nums);

        int res = nums[0];
        for (int i = 1; i < nums.length - 1; i++) {
            int a = nums[i] - nums[i - 1];
            int b = nums[i + 1] - nums[i];
            if (a != 0 && b != 0) {
                res = nums[i];
            }
        }

        if (nums[len - 1] - nums[len - 2] != 0) {
            res = nums[len - 1];
        }

        return res;
    }
}
